package edu.friday.controller;

import edu.friday.common.constant.UserConstants;
import edu.friday.common.result.RestResult;
import edu.friday.common.result.TableDataInfo;
import edu.friday.model.vo.SysUserVO;
import edu.friday.service.SysRoleService;
import edu.friday.service.SysUserService;
import edu.friday.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/system/user")
public class SysUserController extends BaseController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysRoleService sysRoleService;

    /**
     * 显示用户表 所有数据
     */
    @GetMapping("/list")
    public TableDataInfo list(SysUserVO user, Pageable page) {
        int pageNum = page.getPageNumber() - 1;
        pageNum = pageNum <= 0 ? 0 : pageNum;
        page = PageRequest.of(pageNum, page.getPageSize());
        return sysUserService.selectUserList(user, page);
    }

    /**
     * 新增
     * Postram user
     *
     * @return
     */
    @PostMapping
    public RestResult add(@Validated @RequestBody SysUserVO sysUserVO) {
        if (UserConstants.NOT_UNIQUE.equals(sysUserService.checkUserNameUnique(sysUserVO.getUserName()))) {
            return RestResult.error("新增用户'" + sysUserVO.getUserName() + "'失败，登录账号已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(sysUserService.checkPhoneUnique(sysUserVO))) {
            return RestResult.error("新增用户'" + sysUserVO.getUserName() + "'失败，手机号码已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(sysUserService.checkEmailUnique(sysUserVO))) {
            return RestResult.error("新增用户'" + sysUserVO.getUserName() + "'失败，邮箱账号已存在");
        }
        sysUserVO.setCreateBy("leiyu");
        boolean flag = sysUserService.insertUser(sysUserVO);
        return toAjax(flag ? 1 : 0);
    }

    /**
     * 批量删除
     * Postram user
     *
     * @return
     */
    @DeleteMapping("/{userIds}")
    public RestResult delete(@PathVariable Long[] userIds) {
        return toAjax(sysUserService.deleteByUserIds(userIds));
    }

    @PutMapping
    //@Validated数据校验
    public RestResult edit(@Validated @RequestBody SysUserVO sysUserVO) {
        if (UserConstants.NOT_UNIQUE.equals(sysUserService.checkPhoneUnique(sysUserVO))) {
            return RestResult.error("修改用户'" + sysUserVO.getUserName() + "'失败，手机号码已存在");
        } else if (UserConstants.NOT_UNIQUE.equals(sysUserService.checkEmailUnique(sysUserVO))) {
            return RestResult.error("修改用户'" + sysUserVO.getUserName() + "'失败，邮箱账号已存在");
        }
        sysUserVO.setUpdateBy("system");
        boolean flag = sysUserService.updateUser(sysUserVO);
        return toAjax(flag ? 1 : 0);
    }

    @GetMapping(value = {"/", "/{userId}"})
    public RestResult list(@PathVariable(value = "userId", required = false) Long userId) {
        RestResult restResult = RestResult.success();
        restResult.put("roles",sysRoleService.selectRoleAll());
        //required = false(允许为空值（可能为空值）需要判断)
        if(StringUtils.isNotNull(userId)){
            restResult.put("roleIds",sysUserService.selectRoleListByUserId(userId));
            restResult.put(RestResult.DATA_TAG,sysUserService.selectUserById(userId));
        }
        return restResult;
    }
}
